package pers.ascend.dalston.service.governance.eureka.producer.manage.controller;

import com.netflix.discovery.DiscoveryClient;
import com.netflix.discovery.DiscoveryManager;
import com.netflix.discovery.endpoint.EndpointUtils;
import org.hibernate.validator.constraints.NotBlank;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import pers.ascend.dalston.service.governance.eureka.producer.manage.entity.vo.UploadFile2;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 说明：
 *
 * @author ascend
 * 2021/3/5 10:50
 */
@RestController
@RequestMapping(value = "/producer")
public class EurekaProducerController {
    protected final Logger log = LoggerFactory.getLogger(getClass());

    @GetMapping(value = "/dc")
    public List<String> dc() {
        DiscoveryClient client = DiscoveryManager.getInstance().getDiscoveryClient();
        return EndpointUtils.getServiceUrlsFromConfig(client.getEurekaClientConfig(), "defaultZone", true);
    }

    @GetMapping(value = "/hello")
    public String hello(@RequestParam("name") @NotBlank String name) {
        if (log.isInfoEnabled()) {
            log.info("EurekaProducerController.hello()");
        }
        return "hello " + name;
    }

    /**
     * 文件上传1
     *
     * @param file File
     * @return String
     */
    @PostMapping(path = "/upload-file1", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public String uploadFile1(@RequestPart("file") @NotNull MultipartFile file) {
        if (log.isInfoEnabled()) {
            log.info("EurekaProducerController.uploadFile1()");
            log.info("文件名称：" + file.getOriginalFilename());
            log.info("文件类型：" + file.getContentType());
            log.info("文件大小：" + file.getSize());
        }
        return "上传成功";
    }

    /**
     * 文件上传2
     *
     * @param uf2 UploadFile2
     * @return String
     */
    @PostMapping(path = "/upload-file2", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public String uploadFile2(UploadFile2 uf2) {
        if (log.isInfoEnabled()) {
            log.info("EurekaProducerController.uploadFile2()");
            log.info("文件名称：" + uf2.getFile().getOriginalFilename());
            log.info("文件类型：" + uf2.getFile().getContentType());
            log.info("文件大小：" + uf2.getFile().getSize());
            log.info("上传内容：" + uf2);
        }
        return "上传成功";
    }
}

